<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { useI18n } from '@/hooks/web/useI18n'
import { onMounted, reactive, ref } from 'vue'
import { Edit, Plus, Search, Lock, Unlock, Delete } from '@element-plus/icons-vue'
import {
  dictionaryItemName,
  dictionaryItemTag,
  getDictionarySelectOptionList
} from '@/utils/dictionary'
import { SelectOption } from '@/api/common/types'
import { SystemMenu, SystemMenuQuery } from '@/api/system/menu/types'
import { getSystemMenuListApi } from '@/api/system/menu'
import { CreateOrUpdate } from './components'

const { t } = useI18n()
const menuList = ref<SystemMenu[]>([])
const getTableData = async () => {
  const res = await getSystemMenuListApi(queryData)
  if (res) {
    menuList.value = res.data
  }
}

const queryData = reactive<SystemMenuQuery>({
  menuType: '',
  menuName: '',
  menuCode: '',
  menuStatus: ''
})
const listLoading = ref(false)
onMounted(() => {
  getTableData()
})

const createOrUpdateDialogVisible = ref(false)
const systemMenu = ref<SystemMenu>()
const title = ref('新增菜单')
const isCreate = ref(true)
const createMenu = () => {
  createOrUpdateDialogVisible.value = true
  title.value = '新增菜单'
  isCreate.value = true
  systemMenu.value = {
    menuType: '0',
    menuStatus: '1',
    sort: 1
  }
}
const updateMenu = (menu: SystemMenu) => {
  createOrUpdateDialogVisible.value = true
  title.value = '编辑菜单'
  isCreate.value = false
  systemMenu.value = menu
}
const menuStatusList = ref<SelectOption[]>()
const getDictionaryList = async () => {
  menuStatusList.value = getDictionarySelectOptionList('status')
}
getDictionaryList()
</script>

<template>
  <ContentWrap>
    <el-form :inline="true" :model="queryData" class="demo-form-inline">
      <el-form-item label="菜单名称">
        <el-input v-model="queryData.menuName" placeholder="请输入菜单名称" clearable />
      </el-form-item>
      <el-form-item label="菜单状态" style="width: 200px">
        <el-select v-model="queryData.menuStatus" placeholder="请选择菜单状态">
          <el-option label="全部" value="" />
          <el-option
            v-for="item in menuStatusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="getTableData"
          ><el-icon class="el-icon--left"><Search /></el-icon>查询</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="createMenu"
          ><el-icon class="el-icon--left"><Plus /></el-icon>新增</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="menuList"
      v-loading="listLoading"
      row-key="menuId"
      border
      style="width: 100%"
      fit
      stripe
      highlight-current-row
    >
      <el-table-column prop="menuName" align="center" label="菜单名称" width="160" />
      <el-table-column prop="menuType" align="center" label="菜单类型" width="100">
        <template #default="scope">
          <el-tag class="ml-2" :type="dictionaryItemTag('menuType', scope.row.menuType)">
            <span v-if="scope.row.menuType">{{
              dictionaryItemName('menuType', scope.row.menuType)
            }}</span>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="parent.menuName" align="center" label="父菜单" width="160" />
      <el-table-column prop="menuPath" align="center" label="菜单路径" />
      <el-table-column prop="menuCode" align="center" label="菜单编码" width="180" />
      <el-table-column prop="menuStatus" align="center" label="菜单状态" width="100">
        <template #default="scope">
          <el-tag class="ml-2" :type="dictionaryItemTag('status', scope.row.menuStatus)">
            <span v-if="scope.row.menuStatus">{{
              dictionaryItemName('status', scope.row.menuStatus)
            }}</span>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180"
      >
        <template #default="{ row }">
          <el-link type="primary" plain @click="updateMenu(row)">
            <el-icon class="el-icon--right"><Edit /></el-icon>编辑
          </el-link>
          <el-link v-if="row.menuStatus === '1'" type="warning" style="margin-left: 5px" plain>
            <el-icon class="el-icon--right"><Lock /></el-icon> 停用
          </el-link>
          <el-link v-if="row.menuStatus === '0'" type="success" style="margin-left: 5px" plain>
            <el-icon class="el-icon--right"><Unlock /></el-icon>启用
          </el-link>
          <el-link type="danger" style="margin-left: 5px" plain
            ><el-icon class="el-icon--right"><Delete /></el-icon>删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>
  <CreateOrUpdate
    v-if="createOrUpdateDialogVisible"
    v-model="createOrUpdateDialogVisible"
    :menu="systemMenu"
    :title="title"
    :create="isCreate"
    :refreshList="getTableData"
  />
</template>
